package com.eqs.enterprisequestionnaire.service.Impl;

import com.eqs.enterprisequestionnaire.model.pojo.Answer;
import com.eqs.enterprisequestionnaire.model.pojo.Option;
import com.eqs.enterprisequestionnaire.model.pojo.Question;
import com.eqs.enterprisequestionnaire.service.HistoryService;
import com.eqs.enterprisequestionnaire.service.QuestionService;
import com.eqs.enterprisequestionnaire.service.SortQuestionAnalysisService;
import com.eqs.enterprisequestionnaire.service.SortQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import static com.eqs.enterprisequestionnaire.model.enums.QuestionEnum.isSortType;


import java.util.*;

@Service
public class SortQuestionAnalysisImpl implements SortQuestionAnalysisService {
        @Autowired
        private QuestionService questionService;

        @Autowired
        private HistoryService historyService;

        public Map<String, Double> getSortOptionScore(Integer questionId) {
            Question question = questionService.selectByQuestionId(questionId);

            // 判断是否为排序题类型
            if (!isSortType(question.getType())) {
                return null;
            }

            // 获取该问题的所有选项
            List<Option> options = historyService.getListOptionByQuestionId(questionId);

            // 初始化选项得分的Map
            Map<String, Double> optionScoreMap = new HashMap<>();

            // 获取该问题的所有答案
            List<Answer> answers = historyService.getListAnswerByQuestionId(questionId);

            // 计算每个选项的得分
            for (Option option : options) {
                int totalFrequency = 0;
                int totalScore = 0;

                for (Answer answer : answers) {
                    if (Integer.parseInt(answer.getContent().trim()) == option.getId()) {
                        int position = Integer.parseInt(answer.getText());  // 排名
                        int weight = options.size() + 1 - position;  // 权值计算
                        totalFrequency++;
                        totalScore += weight;
                    }
                }

                double averageScore = totalFrequency == 0 ? 0.0 : (double) totalScore / totalFrequency;
                optionScoreMap.put(option.getContent(), averageScore);
            }

            return optionScoreMap;
        }

        public Map<String, List<Double>> getSortOptionRankingProportions(Integer questionId){
            Question question = questionService.selectByQuestionId(questionId);

            // 判断是否为排序题类型
            if (!isSortType(question.getType())) {
                return null;
            }

            // 获取该问题的所有选项
            List<Option> options = historyService.getListOptionByQuestionId(questionId);

            // 初始化选项排名占比的Map
            Map<String, List<Double>> optionRankingProportions = new HashMap<>();

            // 获取该问题的所有答案
            List<Answer> answers = historyService.getListAnswerByQuestionId(questionId);


            // 计算每个选项在每个排名中的占比
            for (Option option : options) {
                int[] rankCounts = new int[options.size()]; // 用于存储各个排名的次数
                int totalRanks = 0; // 用于存储总的排名次数

                for (Answer answer : answers) {
                    if (Integer.parseInt(answer.getContent().trim()) == option.getId()) {
                        int rank = Integer.parseInt(answer.getText())-1 ;
                        rankCounts[rank]++;
                        totalRanks++;
                    }
                }

                List<Double> rankProportions = new ArrayList<>();
                for (int count : rankCounts) {
                    rankProportions.add(totalRanks == 0 ? 0.0 : (double) count / totalRanks);
                }

                optionRankingProportions.put(option.getContent(), rankProportions);
            }

            return optionRankingProportions;
        }
}

